﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using OpenMVCRM.Entities;
using OpenMVCRM.DataAccess;
using System.Diagnostics;

namespace OpenMVCRM.Controllers
{
    public class ImportController : Controller
    {
        private IRepositary repo;
        public ImportController()
        {
            repo = new Repositary();
        }

        public string Index()
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            //XDocument doc = XDocument.Load(Server.MapPath(@"../Content/temp/country_state.xml"));
            //if (doc != null)
            //{
            //    string ss=doc.
            //}

            XElement elm = XElement.Load(Server.MapPath(@"../Content/temp/users.xml"));
            if (elm != null)
            {

                //var reader = elm.CreateReader();
                //reader.MoveToContent();
                //string ss = reader.ReadInnerXml();


                //var users = elm.Descendants("User").ToList();
                //foreach (var user in users)
                //{
                //    var userName =user.Descendants("default").Where(s=>s.Attributes().Contains()).FirstOrDefault();
                // //  =defaultItem.Attribute("UserName").Value;
                //   // string password = defaultItem.Attribute("Password").Value;

                //}



                var countries = elm.Descendants("country");
                foreach (var country in countries)
                {
                    var countryEntity = new Country();
                    countryEntity.CountryName = country.Attribute("name").Value; //Here is the countryname

                    var states = country.Descendants("state");
                    foreach (var state in states)
                    {
                        var stateEntity = new State();
                        stateEntity.StateName = state.Value; //Here is the stateName
                        // countryEntity.States.Add(stateEntity);
                    }
                    var result = repo.InsertCountry(countryEntity);
                    if (result.Status)
                    {
                        //do whatever
                    }
                }


            }
            TimeSpan ts = stopWatch.Elapsed;
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);

            return "country state inserted. Time take : " + elapsedTime;
        }

        public string Index2()
        {

            string filePath = Server.MapPath(@"../Content/temp/country_state.xml");
            XElement elm = XElement.Load(filePath);
            if (elm != null)
            {
                var countries = elm.Descendants("country");
                foreach (var country in countries)
                {
                    var countryName = country.Attribute("name").Value;

                    var states = country.Descendants("state");
                    foreach (var state in states)
                    {
                        var stateName = state.Value;
                    }
                }
            }



            return "country state inserted";
        }

    }
}
